package com.example.neusoftfood_618lw.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.neusoftfood_618lw.R;
import com.example.neusoftfood_618lw.beans.UserBean;
import com.example.neusoftfood_618lw.beans.UserInfo;
import com.example.neusoftfood_618lw.listener.RetrofitListener;
import com.example.neusoftfood_618lw.model.UserModel;

public class LoginActivity extends AppCompatActivity implements RetrofitListener<UserInfo>
{

    private EditText etuser,etpass;
    private Button btnlogin,register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
    }

    private void initViews()
    {
        etuser=findViewById(R.id.etuser);
        etpass=findViewById(R.id.etpass);
        btnlogin=findViewById(R.id.button);
        register=findViewById(R.id.button2);

        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel userModel = new UserModel();
                userModel.userLogin(etuser.getText().toString().trim(),
                        etpass.getText().toString().trim(),LoginActivity.this);
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

    }



    @Override
    public void onSuccess(UserInfo userInfo, int flag) {
        if(!userInfo.getUserid().equals("0"))
            Toast.makeText(LoginActivity.this, "登录成功：",Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
    }
}
